<script setup>
import {
  reactive,
  ref
} from "vue";
  import {
    UserFilled,
    Lock,
  } from '@element-plus/icons-vue'
  import { loginUser} from '@/api/getData.js'
import {ElMessage} from "element-plus";
  /*定义变量开始*/
  //提交表单的数据
  const formData = reactive({
    userName:"",
    password:""
  })
  const rules = ({
    userName: [
      { required: true,
        message: '请输入用户名',
        trigger: 'blur' }
    ],
    password: [
      { required: true,
        message: '请输入密码',
        trigger: 'blur' }
    ],
  })
  //表单验证
  const loginFormRef = ref (null);
  /*定义变量结束*/

  /*定义函数开始*/
  //表单验证函数
  const submitForm = () =>{
    loginFormRef.value.validate((valid) => {
      if (valid) {
        //表单验证通过，请求后端接口进行登录
        console.log("验证成功")
        login()
      }else {
        //表单验证失败
        console.log("验证失败")
      }

    })
  }
  //登录
  const login =()=>{
    loginUser(formData).then(res=>{
      if(res.code === 200){//登录功能
        //登录成功提示
        ElMessage.success("登录成功")
        //保存token，用户信息，用户菜单到浏览器缓存

      }
    })
  }
  /*定义函数结束*/
</script>

<template>
  <div class="login-main">
    <div class="login-content">
      <div class="login-title">欢迎登录</div>
      <div>
        <el-form :model="formData" ref="loginFormRef" :rules="rules">
          <el-form-item prop="username">
            <el-input v-model="formData.userName" placeholder="请输入用户名" :prefix-icon="UserFilled" />
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="formData.password" placeholder="请输入密码" :prefix-icon="Lock" />
          </el-form-item>
          <el-form-item >
            <el-button class="login-button" type="primary" @click="submitForm">登录</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<style scoped>
  .login-main {
    background-color: rgb(45,58,75);
    height: 100%;
    display: flex;
    justify-content: center;
  }
  .login-content{
    width: 500px;
    height: 500px;
    margin-top: 200px;
  }
  .login-title{
    text-align: center;
    color: white;
    font-size: 30px;
    font-weight: bolder;
    letter-spacing: 10px;
    margin-bottom: 40px;
  }
  .login-button{
    width: 100%;
  }
</style>